package router

import (
	"github.com/gin-gonic/gin"
	"github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
	"naio/config"
	"naio/handler/api/v1"
	"naio/handler/backend"
	"naio/middleware"
	"naio/serializer"
	"naio/service"
	"net/http"
)

type App struct {
	config *config.BaseConfig
	router *gin.Engine
}

func New() *App {
	app := &App{}
	app.config = config.Config
	app.router = gin.Default()
	app.registerMiddleware()
	s := service.NewService(app.config)
	app.registerHandler(s)
	return app
}

func (app *App) GetRouter() *gin.Engine {
	return app.router
}

func (app *App) registerMiddleware() {
	app.router.Use(middleware.AccessLog())
	app.router.Use(middleware.NoCache())
	app.router.Use(middleware.Cors())
	app.router.Use(middleware.Secure())
	app.router.Use(middleware.RequestId())
	app.router.Use(middleware.RecoverPanic())
}

func (app *App) registerHandler(s *service.Service) {
	apiHandler := api.NewHandler(app.config, s)
	app.setApiV1(apiHandler)
	app.setMiniAppV1(apiHandler)

	backendHandler := backend.NewHandler(app.config, s)
	app.setBackend(backendHandler)

	app.set404()
	app.setSwagger()
}

func (app *App) setApiV1(apiHandler *api.Handler) {
	app.router.GET("/api/v1/home", apiHandler.Home)
}

func (app *App) setMiniAppV1(apiHandler *api.Handler) {
	app.router.GET("/miniapp/v1/home", apiHandler.Home)
}

func (app *App) setBackend(backendHandler *backend.Handler) {
	app.router.POST("/backend/auth/login", backendHandler.AuthLogin)
	app.router.POST("/backend/auth/refresh", backendHandler.AuthRefreshToken)

	// 以下两个中间件先后顺序不能互换
	auth := app.router.Group("/backend")
	auth.Use(middleware.CheckToken())
	//auth.Use(middleware.CheckPermission())
	{
		auth.GET("/auth/info", backendHandler.AuthInfoFetch)
		auth.GET("/auth/change_password", backendHandler.AuthPasswordChange)
		auth.GET("/auth/menu", backendHandler.AuthMenuFetch)
		auth.GET("/auth/logout", backendHandler.AuthLogout)
		auth.GET("/banner/list", backendHandler.BannerBrowse)
		auth.GET("/banner/id/*id", backendHandler.BannerFetch)
		auth.POST("/banner/edit", backendHandler.BannerUpdate)
		auth.DELETE("/banner/id/*id", backendHandler.BannerDelete)
		auth.GET("/category/list", backendHandler.CategoryBrowse)
		auth.GET("/category/id/*id", backendHandler.CategoryFetch)
		auth.POST("/category/edit", backendHandler.CategoryUpdate)
		auth.DELETE("/category/id/*id", backendHandler.CategoryDelete)
		auth.GET("/grid_category/list", backendHandler.GridCategoryBrowse)
		auth.GET("/grid_category/id/*id", backendHandler.GridCategoryFetch)
		auth.POST("/grid_category/edit", backendHandler.GridCategoryUpdate)
		auth.DELETE("/grid_category/id/*id", backendHandler.GridCategoryDelete)
		//auth.GET("/admin/list", backendHandler.AdminBrowse)
		//auth.GET("/admin/id/*id", backendHandler.AdminFetch)
		//auth.POST("/admin/edit", backendHandler.AdminUpdate)
		//auth.DELETE("/admin/id/*id", backendHandler.AdminDelete)
		//auth.GET("/role/list", backendHandler.RoleBrowse)
		//auth.GET("/role/id/*id", backendHandler.RoleFetch)
		//auth.POST("/role/edit", backendHandler.RoleUpdate)
		//auth.DELETE("/role/id/*id", backendHandler.RoleDelete)
		//auth.GET("/permission/list", backendHandler.PermissionBrowse)
		//auth.GET("/permission/id/*id", backendHandler.PermissionFetch)
		//auth.POST("/permission/edit", backendHandler.PermissionUpdate)
		//auth.DELETE("/permission/id/*id", backendHandler.PermissionDelete)
	}
}

func (app *App) set404() {
	app.router.NoRoute(func(c *gin.Context) {
		res := serializer.SuccessMessage{Code: 404, Msg: "没有这个api接口route", Data: nil}
		res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
		c.JSON(http.StatusOK, res)
	})
	app.router.NoMethod(func(c *gin.Context) {
		res := serializer.SuccessMessage{Code: 404, Msg: "没有这个api接口method", Data: nil}
		res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
		c.JSON(http.StatusOK, res)
	})
}

func (app *App) setSwagger() {
	app.router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
